package com.miyake.client.datamanage;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.miyake.client.DataService;
import com.miyake.client.DataServiceAsync;
import com.miyake.client.DoctorsUserService;
import com.miyake.client.DoctorsUserServiceAsync;
import com.miyake.client.datamanage.plugin.PluginProperty;
import com.miyake.client.tools.ClickableLabel;

public class ShowData {
	private DataServiceAsync service = DataService.Util.getInstance();
	public ShowData(NodeProperty node, Panel panel) {
		final VerticalPanel p = new VerticalPanel();
		panel.add(p);
		service.getData(node.getKey(), new AsyncCallback<Data>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(Data result) {
				showData(result, p);
			}
		});
	}
	protected void showData(final Data result, VerticalPanel p) {
		if (result.getSourceType().equals(Data.SourceTypeFile)) {
			showFileProperty(result, p);
		}
		else if (result.getSourceType().equals(Data.SourceTypeAnalysis)) {
			showAnalysisProperty(result, p);
		}

	}
	protected void showAnalysisProperty(final Data result, VerticalPanel p) {
		HorizontalPanel h = new HorizontalPanel();
		p.add(h);
		h.add(new Label("Analysis:"));
		getPluginName(result.getAnalysisPluginKey(), h);
		//Grid grid = new Grid();
		//p.add(grid);
		//grid.resize(result.getParameters().size(), 3);
		int row = 0;
		for (Parameter param : result.getParameters()) {
			//grid.setText(row, 0, param.getTitle() + ":");
			//grid.setText(row, 1, param.getValue());
			//grid.setText(row, 2, param.getUnit());
			p.add(new Label(param.getTitle() + ":" + param.getValue() + " " + param.getUnit()));
			row++;
		}
	}
	protected void showFileProperty(final Data result, VerticalPanel p) {
		HorizontalPanel h = new HorizontalPanel();
		p.add(h);
		h.add(new Label("File:"));
		h.add(new ClickableLabel(result.getFilename(), new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				Window.open(result.getFileURL(), "", "menubar=no,location=no,resizable=no,scrollbars=yes,status=no,toolbar=no,width=0,height=0,visible=no");
			}
		}));
		
		if (result.isParsed()) {
			final Label label = new Label();
			p.add(label);
			service.getPlugin(result.getFilePluginKey(), new AsyncCallback<PluginProperty>() {
				@Override
				public void onFailure(Throwable caught) {
				}

				@Override
				public void onSuccess(PluginProperty result) {
					label.setText(result.getName());
				}
			});
			p.add(new Label());
		}
		else {
			p.add(new Label("No Parsed"));
		}
	}
	private void getPluginName(String analysisPluginKey, HorizontalPanel p) {
		final Label label = new Label();
		p.add(label);
		service.getPlugin(analysisPluginKey, new AsyncCallback<PluginProperty>() {
			@Override
			public void onFailure(Throwable caught) {

			}

			@Override
			public void onSuccess(PluginProperty result) {
				label.setText(result.getName());
			}
			
		});
		
	}

}
